<?
//------------------------------------------------------------------------------------------------------------------
function get_signups_days(){
        global $session_db_name,$dbh;

        $days = Array();
        for($i=31;$i>0;$i--){
                $days[$i] = date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$i,date("Y")));
        }
        foreach($days as $d)  $global_days[$d]  = "";
    $SQL="select
           count(distinct r.reseller) as count,
           count(distinct c.client) as amount,
           year(r.date) y, month(r.date) m,dayofmonth(r.date)  d, date_add(date_add( date_add(r.date,interval  - second(r.date)  second) , interval - minute(r.date) minute),interval - hour(r.date) hour) as created
         from
           resellers r left join sales c on (r.id=c.id)
         where  r.date>date_add(now(),interval -31 day) group by y,m,d";
    if (!($result = @mysql_db_query($session_db_name, $SQL, $dbh)))
                        return "ERROR: $SQL";
    while($row = @mysql_fetch_array($result)){
                $global_days[$row[created]][count] =  $row[count];
                $global_days[$row[created]][amount] =  $row[amount];
        }
	//plans
	$SQL = "select count(distinct client) as count ,year(date) y, month(date) m,dayofmonth(date)  d, date_add(date_add( date_add(date,interval  - second(date)  second) , interval - minute(date) minute),interval - hour(date) hour) as created  from sales where  date>date_add(now(),interval -31 day) and state='normal' and type='plan' group by y,m,d;";
	if (!($result = @mysql_db_query($session_db_name, $SQL, $dbh)))
			return "ERROR: $SQL";
	while($row = @mysql_fetch_array($result)){
		$global_days[$row[created]][count1] =  $row[count];
	}
	//upgardes
	$SQL = "select count(distinct client) as count ,year(date) y, month(date) m,dayofmonth(date)  d, date_add(date_add( date_add(date,interval  - second(date)  second) , interval - minute(date) minute),interval - hour(date) hour) as created  from sales where  date>date_add(now(),interval -31 day) and state='normal' and type='upgrade' group by y,m,d;";
	if (!($result = @mysql_db_query($session_db_name, $SQL, $dbh)))
			return "ERROR: $SQL";
	while($row = @mysql_fetch_array($result)){
		$global_days[$row[created]][count3] =  $row[count];
	}
	//refunds
	$SQL = "select count(distinct client) as count ,year(date) y, month(date) m,dayofmonth(date)  d, date_add(date_add( date_add(date,interval  - second(date)  second) , interval - minute(date) minute),interval - hour(date) hour) as created  from sales where  date>date_add(now(),interval -31 day) and state!='normal'  group by y,m,d;";
        if (!($result = @mysql_db_query($session_db_name, $SQL, $dbh)))
                        return "ERROR: $SQL";
        while($row = @mysql_fetch_array($result)){
                $global_days[$row[created]][count2] =  $row[count];
        }

        $p = 0;
        while(list($key,$gd) = each($global_days)){
                $key = explode("-",$key);
                $key_ = explode(" ",$key[2]);
                $ddd[data1][$p][date] = "$key[1]/$key_[0]";
                $ddd[data1][$p][count] = $gd[count];
                $ddd[data2][$p][date] = "$key[1]/$key_[0]";
                $ddd[data2][$p][count] = $gd[amount];
		$ddd[data3][$p][date] = "$key[1]/$key_[0]";
	   	$ddd[data3][$p][count] = $gd[count1];
                $ddd[data4][$p][date] = "$key[1]/$key_[0]";
                $ddd[data4][$p][count] = $gd[count2];
                $ddd[data5][$p][date] = "$key[1]/$key_[0]";
                $ddd[data5][$p][count] = $gd[count3];

                $p++;
        }
        return $ddd;
}
//-------------------------------------------------------------------------------------
function get_signups_hours(){
        global $session_db_name,$dbh;

        $hours = Array();
        for($i=24;$i>0;$i--){
                $hours[$i] = date("Y-m-d H:i:s",mktime(date("H")-$i,0,0,date("m"),date("d"),date("Y")));
        }
        foreach($hours as $d)  $global_hours[$d]  = "";

        $SQL="select
               count(distinct r.reseller) as count,
               count(distinct c.client) as amount,
               year(r.date) y, month(r.date) m,dayofmonth(r.date)  d,hour(r.date) h, date_add( date_add(r.date,interval  - second(r.date)  second) , interval - minute(r.date) minute) as created
               from
               resellers r left join sales c on (r.id=c.id)
              where  r.date>date_add(now(),interval -1 day) group by y,m,d,h";

    if (!($result = @mysql_db_query($session_db_name, $SQL, $dbh)))
                        return $SQL;
        while($row = @mysql_fetch_array($result)){
                $global_hours[$row[created]][count] =  $row[count];
                $global_hours[$row[created]][amount] =  $row[amount];
        }
	//plans
	$SQL = "select count(distinct client) as count ,year(date) y, month(date) m,dayofmonth(date)  d, hour(date) h, date_add( date_add(date,interval  - second(date)  second) , interval - minute(date) minute) as created  from sales where  date>date_add(now(),interval - 1 day) and state='normal'  and type='plan' group by y,m,d,h;";
	if (!($result = @mysql_db_query($session_db_name, $SQL, $dbh)))
			return "ERROR: $SQL";
	while($row = @mysql_fetch_array($result)){
		$global_hours[$row[created]][count1] =  $row[count];
	}
	//upgrades
	$SQL = "select count(distinct client) as count ,year(date) y, month(date) m,dayofmonth(date)  d, hour(date) h, date_add( date_add(date,interval  - second(date)  second) , interval - minute(date) minute) as created  from sales where  date>date_add(now(),interval - 1 day) and state='normal'  and type='upgrade' group by y,m,d,h;";
	if (!($result = @mysql_db_query($session_db_name, $SQL, $dbh)))
			return "ERROR: $SQL";
	while($row = @mysql_fetch_array($result)){
		$global_hours[$row[created]][count3] =  $row[count];
	}
	//refunds
	$SQL = "select count(distinct client) as count ,year(date) y, month(date) m,dayofmonth(date)  d, hour(date) h, date_add( date_add(date,interval  - second(date)  second) , interval - minute(date) minute) as created  from sales where  date>date_add(now(),interval - 1 day) and state!='normal'  group by y,m,d,h;";
	if (!($result = @mysql_db_query($session_db_name, $SQL, $dbh)))
			return "ERROR: $SQL";
	while($row = @mysql_fetch_array($result)){
		$global_hours[$row[created]][count2] =  $row[count];
	}

        $p = 0;
        while(list($key,$gd) = each($global_hours)){
                $buff = explode(" ",$key);		
                $key = explode(":",$buff[1]);
		$buff = explode("-",$buff[0]);
                $ddd[data1][$p][date] = "$buff[2]-$key[0]";
                $ddd[data1][$p][count] = $gd[count];
                $ddd[data2][$p][date] = "$buff[2]-$key[0]";
                $ddd[data2][$p][count] = $gd[amount];
                $ddd[data3][$p][date] = "$buff[2]-$key[0]";
                $ddd[data3][$p][count] = $gd[count1];
                $ddd[data4][$p][date] = "$buff[2]-$key[0]";
                $ddd[data4][$p][count] = $gd[count2];
                $ddd[data5][$p][date] = "$buff[2]-$key[0]";
                $ddd[data5][$p][count] = $gd[count3];


                $p++;
        }
        return $ddd;
}
//------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
function get_signups_months(){
        global $session_db_name,$dbh;

        $months = Array();
        for($i=23;$i>0;$i--){
                $months[$i] = date("Y-m-d H:i:s",mktime(0,0,0,date("m")-$i,'01',date("Y")));
        }
        $global_months = Array();
        foreach($months as $d)  $global_months[$d]  = "";
    $SQL="select
              count(distinct r.reseller) as count,
              count(distinct c.client) as amount,
              year(r.date) y, month(r.date) m,date_add(date_add(date_add(date_add(r.date,interval  - second(r.date)  second) , interval - minute(r.date) minute),interval - hour(r.date) hour),interval - (dayofmonth(r.date)-1) day) as creat

            from
              resellers r left join sales c on (r.id=c.id)
              where  r.date>date_add(now(),interval -2 year) group by y,m";

        if (!($result = @mysql_db_query($session_db_name, $SQL, $dbh)))
                        return "ERROR:$SQL";
        while($row = @mysql_fetch_array($result)){
                $global_months[$row[creat]][count] =  $row[count];
                $global_months[$row[creat]][amount] =  $row[amount];
        }
	//plans
	$SQL = "select count(distinct client) as count ,year(date) y, month(date) m,date_add(date_add(date_add(date_add(date,interval  - second(date)  second) , interval - minute(date) minute),interval - hour(date) hour),interval - (dayofmonth(date)-1) day) as creat from sales where  date>date_add(now(),interval - 2 year) and state='normal' and type='plan'  group by y,m;";
	if (!($result = @mysql_db_query($session_db_name, $SQL, $dbh)))
			return "ERROR: $SQL";
	while($row = @mysql_fetch_array($result)){
		$global_months[$row[creat]][count1] =  $row[count];
	}
	//upgrades
	$SQL = "select count(distinct client) as count ,year(date) y, month(date) m,date_add(date_add(date_add(date_add(date,interval  - second(date)  second) , interval - minute(date) minute),interval - hour(date) hour),interval - (dayofmonth(date)-1) day) as creat from sales where  date>date_add(now(),interval - 2 year) and state='normal' and type='upgrade'  group by y,m;";
	if (!($result = @mysql_db_query($session_db_name, $SQL, $dbh)))
			return "ERROR: $SQL";
	while($row = @mysql_fetch_array($result)){
		$global_months[$row[creat]][count3] =  $row[count];
	}
	//refunds
	$SQL = "select count(distinct client) as count ,year(date) y, month(date) m,date_add(date_add(date_add(date_add(date,interval  - second(date)  second) , interval - minute(date) minute),interval - hour(date) hour),interval - (dayofmonth(date)-1) day) as creat from sales where  date>date_add(now(),interval - 2 year)  and state!='normal'  group by y,m;";
	if (!($result = @mysql_db_query($session_db_name, $SQL, $dbh)))
			return "ERROR: $SQL";
	while($row = @mysql_fetch_array($result)){
		$global_months[$row[creat]][count2] =  $row[count];
	}

        $p = 0;
        while(list($key,$gd) = each($global_months)){
                $key = explode("-",$key);
                $ddd[data1][$p][date] = "$key[1]";
                $ddd[data1][$p][count] = $gd[count];
                $ddd[data2][$p][date] = "$key[1]";
                $ddd[data2][$p][count] = $gd[amount];
                $ddd[data3][$p][date] = "$key[1]";
                $ddd[data3][$p][count] = $gd[count1];
                $ddd[data4][$p][date] = "$key[1]";
                $ddd[data4][$p][count] = $gd[count2];
                $ddd[data5][$p][date] = "$key[1]";
                $ddd[data5][$p][count] = $gd[count3];

                $p++;
        }
        return $ddd;
}
//------------------------------------------------------------------------------------------------------------------
$ddd = get_signups_hours();
$ddd[width]='1000';
$ddd[height] ='400';
$ddd[ycount] = '10';
$ddd[details] = 'aa';
$d1 = $ddd;

$ddd = get_signups_days();
$ddd[width]='1000';
$ddd[height] ='400';
$ddd[ycount] = '10';
$ddd[details] = 'aa';
$d2 = $ddd;

$ddd = get_signups_months();
$ddd[width]='1000';
$ddd[height] ='400';
$ddd[ycount] = '10';
$ddd[details] = 'aa';
$d3 = $ddd;

$bd2 = $d2; $bd2[height] = 768; $bd2[width] = 1280;
$bd3 = $d3; $bd3[height] = 768; $bd3[width] = 1280;
//print_r($d1);
//print_r($d2);
//print_r($d3);
?>

<CENTER>
<TABLE border=1 class='bTable' cellspacing='0'>
        <TR>
                <TH>LAST 24 HOURS</TH>
        </TR>
        <TR>
                <TD><img src='graphics/chart.php?<?=encode_params($d1);?>'> </TD>
        </TR>
        <TR>
                <TH>LAST 30 DAYS</TH>
        </TR>
        <TR>
                <TD><a href='graphics/chart.php?<?=encode_params($bd2);?>'><img border=0 src='graphics/chart.php?<?=encode_params($d2);?>'></a></TD>
        </TR>
        <TR>
                <TH>LAST 24 MONTHS</TH>
        </TR>
        <TR>
                <TD><a href='graphics/chart.php?<?=encode_params($bd3);?>'><img border=0 src='graphics/chart.php?<?=encode_params($d3);?>'></a></TD>
        </TR>
</TABLE>
</center>
